/**
 * Created by luoxiaoming on 16-12-27.
 */
Ext.Ajax.request({
    url: 'eval/loadEvalPageData',
    async: false,
    success: function (response) {
        var res = Ext.decode(response.responseText).data;
        var questions = res.questions;

        var scoreCombobox = {
            xtype: 'combobox',
            name: 'score',
            editable: false,
            store: Ext.data.StoreManager.lookup('gradeScoreStore'),
            queryMode: 'remote',
            displayField: 'name',
            valueField: 'code',
            allowBlank: false
        };
        for (var i = 0; i < questions.length; i++) {
            var el = questions[i];
            var fields = new Array();
            fields.push("masterId");
            fields.push("deptCode");
            fields.push("deptName");
            var columns = new Array();
            columns.push({
                "dataIndex": "masterId",
                "width": 50,
                flex: 1,
                "text": "评价主记录代码"
            });
            columns.push({
                "dataIndex": "deptCode",
                "width": 50,
                flex: 1,
                "text": "科室代码"
            });
            columns.push({
                "dataIndex": "deptName",
                "width": 50,
                flex: 1,
                "text": "科室名称"
            });
            for (var s = 0; s <questions.length; s++) {
                columns.push({
                    "dataIndex": questions[s].id,
                    "text": "第 " + (s + 1) + " 个问题",
                    flex: 1,
                    editor: scoreCombobox,
                    renderer: function (val) {
                        if (val == null || val == "") {
                        } else {
                            return Ext.data.StoreManager.lookup('gradeScoreStore').findRecord('code', val).get('name');
                        }
                    }
                });
                fields.push(questions[s].id + "");
            }
            fields.push("mark");
            columns.push({
                "dataIndex": "mark",
                "width": 50,
                flex: 1,
                "text": "备注",
                editor: {
                    xtype: 'textfield',
                    name: 'mark'
                }
            });
            content.add({
                title: el.deptGroup.name,
                xtype: 'panel',
                layout: 'anchor',
                items: [{
                    border: false,
                    xtype: 'grid',
                    region: 'center',
                    itemId: 'questions',
                    split: true,
                    store: Ext.create('Ext.data.Store', {
                        fields: ['id', 'content', 'status'],
                        data: el.questions
                    }),
                    columns: [
                        {
                            text: "题号",
                            width: '10%',
                            sortable: true,
                            dataIndex: 'id'
                        },
                        {
                            text: "内容",
                            width: '80%',
                            flex: 1,
                            sortable: true,
                            dataIndex: 'content'
                        }
                    ]
                }, {
                    xtype: 'grid',
                    store: Ext.create('Ext.data.Store', {
                        fields: fields,
                        data: el.scoreRecords
                    }),
                    columns: columns,
                    autoScroll: true,
                    columnLines: true,
                    rowLines: true,
                    selType: 'cellmodel',
                    plugins: [Ext.create('Ext.grid.plugin.CellEditing', {
                        clicksToEdit: 1,
                        listeners: {
                            edit: function (editor, e) {
                                if (e.originalValue != e.value) {
                                    var target = e.record.data.deptCode;
                                    var questionId = e.field;
                                    var score = e.value;
                                    Ext.Ajax.request({
                                        url: 'eval/updateEval',
                                        params: {
                                            target: target,
                                            questionId: questionId,
                                            score: score
                                        },
                                        success: function (response) {
                                            var res = Ext.decode(response.responseText);
                                            e.record.data[e.field] = e.value;
                                            e.record.commit();
                                        }
                                    })
                                }
                            }
                        }
                    })]
                }]
            });
        }
    }
});


var tt = {
    "details": [{
        "id": 61,
        "questionId": 6,
        "masterId": "2ab1d4d1-d063-4920-bc8a-d1e6f1e04876",
        "grade": null,
        "mark": null
    }, {
        "id": 62,
        "questionId": 7,
        "masterId": "2ab1d4d1-d063-4920-bc8a-d1e6f1e04876",
        "grade": null,
        "mark": null
    }, {
        "id": 63,
        "questionId": 8,
        "masterId": "2ab1d4d1-d063-4920-bc8a-d1e6f1e04876",
        "grade": null,
        "mark": null
    }, {
        "id": 64,
        "questionId": 9,
        "masterId": "2ab1d4d1-d063-4920-bc8a-d1e6f1e04876",
        "grade": null,
        "mark": null
    }, {
        "id": 65,
        "questionId": 10,
        "masterId": "2ab1d4d1-d063-4920-bc8a-d1e6f1e04876",
        "grade": null,
        "mark": null
    }, {
        "id": 66,
        "questionId": 6,
        "masterId": "d577a2d9-3090-4651-b562-165160faf2b7",
        "grade": null,
        "mark": null
    }, {
        "id": 67,
        "questionId": 7,
        "masterId": "d577a2d9-3090-4651-b562-165160faf2b7",
        "grade": null,
        "mark": null
    }, {
        "id": 68,
        "questionId": 8,
        "masterId": "d577a2d9-3090-4651-b562-165160faf2b7",
        "grade": null,
        "mark": null
    }, {
        "id": 69,
        "questionId": 9,
        "masterId": "d577a2d9-3090-4651-b562-165160faf2b7",
        "grade": null,
        "mark": null
    }, {"id": 70, "questionId": 10, "masterId": "d577a2d9-3090-4651-b562-165160faf2b7", "grade": null, "mark": null}],
    "questions": [{"content": "内部互评6\n", "id": 6, "mark": "", "type": "A"}, {
        "content": "内部互评7\n",
        "id": 7,
        "mark": "",
        "type": "A"
    }, {"content": "内部互评8\n", "id": 8, "mark": "", "type": "A"}, {
        "content": "内部互评9\n\n",
        "id": 9,
        "mark": "",
        "type": "A"
    }, {"content": "内部互评10\n", "id": 10, "mark": "", "type": "A"}],
    "masters": [{
        "reporter": null,
        "deptName": "信息科",
        "id": "2ab1d4d1-d063-4920-bc8a-d1e6f1e04876",
        "targetDeptCode": "0021",
        "targetDeptName": "检验科",
        "status": "0",
        "score": null,
        "month": "2017-03",
        "beginTime": null,
        "deptCode": "0011",
        "endTime": null,
        "generatedTime": 1489420800000
    }, {
        "reporter": null,
        "deptName": "信息科",
        "id": "d577a2d9-3090-4651-b562-165160faf2b7",
        "targetDeptCode": "0022",
        "targetDeptName": "放射科",
        "status": "0",
        "score": null,
        "month": "2017-03",
        "beginTime": null,
        "deptCode": "0011",
        "endTime": null,
        "generatedTime": 1489420800000
    }]
}